#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
  unordered_map<ll,ll> hash;
  for(int i=0;i<=10;++i)
    hash[i]=0;

  hash[0]=1;
  hash[4]=1;
  hash[6]=1;
  hash[8]=2;
  hash[9]=1;
  ll n=0;
  cin>>n;
  vector<string> ret(n);
  for(int i=0;i<n;++i)
  {
    cin>>ret[i];
  }
  sort(ret.begin(),ret.end(),[&](const string& a,const string& b){
    ll a1=0;
    ll b1=0;
    for(auto& cha:a)
    {
        a1+=hash[(cha-'0')];
    }
    for(auto& chb:b)
    {
        b1+=hash[(chb-'0')];
    }
    if(a1!=b1)
      return a1<b1;
    else
      return stoi(a)<stoi(b);
  });
  for(auto& ch:ret)
  {
    cout<<ch<<' ';
  }
  return 0;
}
